<?php
class DatabaseHandler {
	//Hold an instance of PDO Class
	private static $_mHandler;
	//private constructor to prevent direct creation of object 
	private function __construct()
	{
		
	} 
	//Return initailized database handler 
	private static function GetHandler(){
	// Create database connection only one dosnt already exits 
		if(!isset(self::$_mHandler))
		{
			
			//Execute code catching potential Exception
			try{
				//Create new PDO class 
				self::$_mHandler =new PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD, array(PDO::ATTR_PERSISTENT=>DB_PERSISTENCY));
				
				//Configure PDO to throw Exception
				self::$_mHandler->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
				
			    }catch(PDOException $e)
			    {
				//Close  the database Handler and trigger Error
				self::Close();
				trigger_error($e->getMessage(),E_USER_ERROR);
				
			    }
		 }	    
			//Return the database Handler 
			return self::$_mHandler;
			
		}
	
	//Clear the PDO class instance
	public static function Close(){
		
		self::$_mHandler=null;
	}
		

	
	// Method for pdo execute 
	public static function Execute($SqlQuery,$params=null){
		
		//Try to execute sql query or stored procedure
		try{
			//Get database Hnadler 
			$database_handler =self::GetHandler();
			
			//prepare query for excecution 
			$statement_handler =$database_handler->prepare($SqlQuery);
			
			//Execute the query
			$statement_handler->execute($params);
		
		}catch(PDOException $e){
			//Close the database and trigger error 
			self::Close();
			trigger_error($e->getMessage(),E_USER_ERROR);
		}
		return $statement_handler->rowCount();
	}
	
	
	public static function GetAll($SqlQuery,$params=null,$fetch_style=PDO::FETCH_ASSOC){
		
		//Initailise the return value to null 
		$result =null;
		
		//Try to execute sql query or stored procedure
		try{
			//Get database handler 
			$database_handler =self::GetHandler();
			
			//Prepare the query of execution
			$statement_handler =$database_handler->prepare($SqlQuery);
			
			//Execute the query
			$statement_handler->execute($params);
			
			//Fetch result
			$result =$statement_handler->fetchAll($fetch_style);
			
		    }catch(PDOException $e){
		    //Close the databse handler and trigger an error 
		    self::Close();
		    trigger_error($e->getMessage(),E_USER_ERROR);	
		    } 
		    //Return the query result
		    return $result;
	}
	
	
	public static function GetRow($SqlQuery,$params=null,$fetch_style=PDO::FETCH_ASSOC){
		
		//Initailise the return value to null 
		$result =null;
		
		//Try to execute sql query or stored procedure
		try{
			//Get database handler 
			$database_handler =self::GetHandler();
			
			//Prepare the query of execution
			$statement_handler =$database_handler->prepare($SqlQuery);
			
			//Execute the query
			$statement_handler->execute($params);
			
			//Fetch result
			$result =$statement_handler->fetch($fetch_style);
			
		    }catch(PDOException $e){
		    //Close the databse handler and trigger an error 
		    self::Close();
		    trigger_error($e->getMessage(),E_USER_ERROR);	
		    } 
		    //Return the query result
		    return $result;
	}
	
	public static function GetOne($SqlQuery,$params=null,$fetch_style=PDO::FETCH_NUM){
		
		//Initailise the return value to null 
		$result =null;
		
		//Try to execute sql query or stored procedure
		try{
			//Get database handler 
			$database_handler =self::GetHandler();
			
			//Prepare the query of execution
			$statement_handler =$database_handler->prepare($SqlQuery);
			
			//Execute the query
			$statement_handler->execute($params);
			
			//Fetch result
			$result =$statement_handler->fetch($fetch_style);
			
			//Save the first value of result set 
			$result =$result[0];
			
		    }catch(PDOException $e){
		    //Close the databse handler and trigger an error 
		    self::Close();
		    trigger_error($e->getMessage(),E_USER_ERROR);	
		    } 
		    //Return the query result
		    return $result;
	}
	
	//Bind the parameter 
	public static function GetAllBind($SqlQuery,$params=null,$fetch_style=PDO::FETCH_ASSOC){
		
		//Initailise the return value to null 
		$result =null;
		
		//Try to execute sql query or stored procedure
		try{
			//Get database handler 
			$database_handler =self::GetHandler();
			
			//Prepare the query of execution
			$statement_handler =$database_handler->prepare($SqlQuery);
			
			//Bind the Parameter
			self::PDOBind($statement_handler,$params) ;
     
            //Execute the query
			$statement_handler->execute();
			
			//Fetch result
			$result =$statement_handler->fetchAll($fetch_style);
			
		    }catch(PDOException $e){
		    //Close the databse handler and trigger an error 
		    self::Close();
		    trigger_error($e->getMessage(),E_USER_ERROR);	
		    } 
		    //Return the query result
		    return $result;
	}
	
    // Bind the parameters in the array 
	public static function PDOBind(&$poStatement, &$paArray){ 
    
		    foreach ($paArray as $k=>$v){
            @$poStatement->bindValue($k,$v[0],$v[1]);
         //   echo "**********";
         //   echo $k;echo"--";echo $v[0];echo"--"; echo $v[1];
         //   echo "***********";
            } 
           // print_r($paArray);
	       }
	
}